import com from './com.js'
import api from './api.js'

let updateDelay = 2000
let updateTimerIndex = 0
let updateStatus = false
export default {
		src: "",
		allSrc: [],
		init(cbFunc) {
			let locData = com.getLocDataObject('srcs' + this.src)
			if (!com.isTrue(locData)) {
				updateStatus = true
				api.get('/api/me/zanTos', {
					src
				}, res => {
					this.allSrc = res.d
					cbFunc()
					com.setLocData('srcs' + this.src, this.allSrc)
				})
			}else{
				this.allSrc = locData?locData:[]
				cbFunc()
			}
		},
		setSrc(src){
			this.src = src
			return this
		},
		zan(srcId) {
			let curSrc = this.src + '/' + srcId
			let status = true
			let locData = com.getLocDataObject('srcs' + this.src)
			if (com.isInArray(curSrc, locData)) {
				this.allSrc = com.arrayDelValue(locData, curSrc)
				status = false
			} else {
				if(!com.isTrue(this.allSrc))this.allSrc=[]
				console.log(this.allSrc)
				this.allSrc.push(curSrc)
			}
			//延迟更新网络数据
			clearTimeout(updateTimerIndex)
			updateTimerIndex = setTimeout(() => {
				//更新动作
				api.post('/api/zanTos/update', {
					src:this.src,
					list: this.allSrc
				}, res => {
					console.log(res)
				})
			}, updateDelay)
			com.setLocData('srcs' + this.src, this.allSrc)
			return status
		},
		check(srcId) {
			let curSrc = this.src + '/' + srcId
			if (com.isInArray(curSrc, this.allSrc)) {
				return true
			} else {
				return false
			}
		}
	}